<?php

namespace App\Modules\Apicrzs\Services\Pay;

use App\Library\Junda\src\CardService;
use App\Library\Junda\src\EquipmentService;
use App\Library\Wechat\PayWeChatApi;
use App\Modules\Admin\Entities\Equipment\EquipmentParamsFreeModel;
use App\Modules\Admin\Entities\Member\UserCardModel;
use App\Modules\Admin\Entities\Water\WaterOrderModel;
use App\Modules\Apicrzs\Services\BaseServices;
use App\Modules\Apicrzs\Services\Water\WaterOrderService;
use app\services\pay\PayServices;

class WaterOrderSuccessServices extends BaseServices
{


    /**
     *  打水
     *  处理 微信 支付宝 在线支付 回调 修改
     *  下发打水
     * @param $orderInfo
     * @param $paytype
     * @param $other
     * @return bool|string[]|void
     */
    public function paySuccessConsume($orderInfo, $paytype = 'weixin', $other = []){
        xdebug($orderInfo,'WaterOrderSuccessServices -api','demo');
        $update_order =[
            'pay_type'=>$paytype,
            'pay_time'=>time(),
            'status'=>1
        ];
        $order_sn = $orderInfo['order_sn'];
        $card_no = $orderInfo['card_no'];
        if ($other && isset($other['trade_no'])) {
            $update_order['trade_no'] = $other['trade_no'];
            $update_order['desc'] = $other['desc'];
        }
        try {
            WaterOrderModel::getInstance()->updateWaterOrderByOrderId($orderInfo['order_id'],$update_order);
            //消费
            // 查询 设备  相关费率
            $paramsFreeInfo = EquipmentParamsFreeModel::getInstance()->getWaterDeviceIdByType($orderInfo['water_id'],$orderInfo['apparatus_type']);
            // 下发打水
            if($orderInfo['status']!=1){
                $res_dashui = $this->IssueOnlineConsume($paramsFreeInfo['equipment_id'],$paramsFreeInfo['money']*100,$order_sn);
            }
            xdebug($update_order,' 扫码打水- 骏达 -处理结果-api','','IC/consume');
            return ['code' => 'SUCCESS','data'=>$res_dashui??[], 'message' => '成功'];
        } catch (\Exception $e) {
            return false;
        }
    }
    // 执行下发打水
    protected function IssueOnlineConsume($equipment_id,$money,$order_no)
    {
        // 下发 指令取水
        $IssueOnline =  EquipmentService::IssueOnlineConsume($equipment_id,$money,$order_no);
        if ($IssueOnline['success']){
            $this->error = "下发成功，点击按键取水";
            return true;
        }
        return $IssueOnline;
    }

    /**
     *  充值套餐
     * @param $orderInfo
     * @param $paytype
     * @param $other
     * @return void
     */
    public function paySuccessRecharge($orderInfo, $paytype = 'weixin', $other = [])
    {
        $update_order =['pay_type'=>$paytype, 'pay_time'=>time(), 'status'=>1];
        try {
            WaterOrderModel::getInstance()->updateWaterOrderByOrderId($orderInfo['order_id'],$update_order);
            $card_no = $orderInfo['card_no'];
            $card_info = UserCardModel::getInstance()->getUserByCardNo($card_no);
            //充值
            $remaining_value = bcadd($card_info['remaining_value'] + $orderInfo['total_price'],2);
            $update_card = [
                'balance'=>$card_info['remaining_value'],
                'remaining_value'=>$remaining_value
            ];

            UserCardModel::getInstance()->updateUserByCardId($card_info['user_card_id'],$update_card);
            $res_jd = CardService::ICAccountCharge($card_no,$orderInfo['total_price'],$orderInfo['amount']);
            xdebug(array_merge($update_card,$res_jd),'水卡充值 - 骏达 -处理结果-api','','IC/callback');
            return ['code' => 'SUCCESS', 'message' => '成功'];
        } catch (\Exception $e) {
            return false;
        }
    }

}
